#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include "tipos.h"

#define MAXKEYS 5
#define MINKEYS (MAXKEYS / 2)
#define NIL (-1)

/// Tipo de dado que quero colocar nas páginas


typedef long rrn_t;

typedef enum
{
  YES = 1,
  NO = 0,
  ERROR = -1
} promo_t;

typedef struct btpage_t
{
  int keycount; // número de chaves armazrnadas na página
  ChavePrimaria key[MAXKEYS]; // chaves armazenadas
  rrn_t child[MAXKEYS + 1]; // rrns dos filhos
} btpage_t;

promo_t btinsert(FILE *file, rrn_t rrn, ChavePrimaria key, rrn_t *promo_r_child, ChavePrimaria *promo_key);

void btread(FILE *file, rrn_t rrn, btpage_t *page_ptr);

void btwrite(FILE *file, rrn_t rrn, btpage_t *page_ptr);

void ins_in_page(ChavePrimaria key, rrn_t child, btpage_t *page_ptr);

int search_node(ChavePrimaria key, btpage_t *page_ptr, rrn_t *pos);

void split(FILE *file, ChavePrimaria key, rrn_t r_child, btpage_t *p_oldpage, ChavePrimaria *promo_key, rrn_t *promo_r_child, btpage_t *p_newpage);

void init_btpage(btpage_t *page);

ChavePrimaria no_key();

rrn_t get_btpage(FILE *file);

rrn_t get_root(FILE *file);

rrn_t btcreate_root(FILE *file, ChavePrimaria key, rrn_t left, rrn_t right);

void save_root(FILE *file, rrn_t root);

void list_btree(FILE *file);

